﻿using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;

//创建时间：2025-09-12
namespace ZR.Admin.WebApi.Controllers {
    /// <summary>
    /// 批次追溯
    /// </summary>
    [Verify]
    [Route("business/CksBatchCodeData")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksBatchCodeDataController : BaseController {
        /// <summary>
        /// 批次追溯接口
        /// </summary>
        private readonly ICksBatchCodeDataService _CksBatchCodeDataService;

        public CksBatchCodeDataController(ICksBatchCodeDataService CksBatchCodeDataService) {
            _CksBatchCodeDataService = CksBatchCodeDataService;
        }

        /// <summary>
        /// 查询批次追溯列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "cksbatchcodedata:list")]
        public IActionResult QueryCksBatchCodeData([FromQuery] CksBatchCodeDataQueryDto parm) {
            var response = _CksBatchCodeDataService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询批次追溯详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "cksbatchcodedata:query")]
        public IActionResult GetCksBatchCodeData(int Id) {
            var response = _CksBatchCodeDataService.GetInfo(Id);

            var info = response.Adapt<CksBatchCodeDataDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加批次追溯
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "cksbatchcodedata:add")]
        [Log(Title = "批次追溯", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksBatchCodeData([FromBody] CksBatchCodeDataDto parm) {
            var modal = parm.Adapt<CksBatchCodeData>().ToCreate(HttpContext);

            var response = _CksBatchCodeDataService.AddCksBatchCodeData(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新批次追溯
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "cksbatchcodedata:edit")]
        [Log(Title = "批次追溯", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksBatchCodeData([FromBody] CksBatchCodeDataDto parm) {
            var modal = parm.Adapt<CksBatchCodeData>().ToUpdate(HttpContext);
            var response = _CksBatchCodeDataService.UpdateCksBatchCodeData(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除批次追溯
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "cksbatchcodedata:delete")]
        [Log(Title = "批次追溯", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksBatchCodeData([FromRoute] string ids) {
            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_CksBatchCodeDataService.Delete(idArr));
        }

        /// <summary>
        /// 清空批次追溯
        /// </summary>
        /// <returns></returns>
        [Log(Title = "批次追溯", BusinessType = BusinessType.CLEAN)]
        [ActionPermissionFilter(Permission = "cksbatchcodedata:delete")]
        [HttpDelete("clean")]
        public IActionResult Clear() {
            if (!HttpContextExtension.IsAdmin(HttpContext)) {
                return ToResponse(ResultCode.FAIL, "操作失败");
            }
            return SUCCESS(_CksBatchCodeDataService.TruncateCksBatchCodeData());
        }

    }
}